﻿CREATE FUNCTION dbo.getPaySumCurByDate
	(
	@PUId int,
	@PayDate date
	)
RETURNS money
AS
	BEGIN

	DECLARE @paySum money

	SELECT @paySum = SUM(dbo.PAYMENT_IN_ITEM.SUM_CUR) from (dbo.PAYMENT_IN_ITEM INNER JOIN dbo.PAYMENT_IN ON dbo.PAYMENT_IN_ITEM.PAYMENT_IN_ID = dbo.PAYMENT_IN.ID) 
	WHERE dbo.PAYMENT_IN_ITEM.PRODUCT_UNIT_ID=@PUId  AND dbo.PAYMENT_IN.DATE < @PayDate 
	
	RETURN @paySum
	END